//
//  ResetPasswordViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/23.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import "HWZBAPIRequest.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "NSString+Utils.h"
#import "RegexKit.h"
#import "MBProgressHUD+Toast.h"

@interface ResetPasswordViewController ()

@property (strong, nonatomic) UITextField *passwordTextField;
@property (strong, nonatomic) UIButton *commitButton;

@end

@implementation ResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_WHITE;
    self.navigationItem.title = @"重新设置密码";
    [self setBackNavgationItem];
    
    [self createUI];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UIButton Action
- (void)commitButtonAction:(id)sender {
    
    [self.passwordTextField resignFirstResponder];
    NSString *mobile = self.mobilePhone;
    NSString *verifyCode = self.verifyCode;
    NSString *password = self.passwordTextField.text;
    if([NSString isBlankString:password]) {
        [MBProgressHUD showToast:@"请输入密码" initWithView:self.view];
        return;
    } else if(password.length < 6 || password.length > 18) {
        [MBProgressHUD showToast:@"密码过短，正常密码长度为6~18位" initWithView:self.view];
        return;
    }
    //忘记密码
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:mobile forKey:@"mobile"];
    [params setObject:verifyCode forKey:@"checkRand"];
    [params setObject:password forKey:@"password"];
    [SVProgressHUD showWithStatus:@"正在修改密码..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] forgetPasswordWithParams:params success:^(CommonInfo *resultInfo) {
        [SVProgressHUD dismiss];
        if(resultInfo != nil && [resultInfo isSuccess]) {
            [MBProgressHUD showToast:@"修改密码成功" initWithView:self.view];
            [self performSelector:@selector(dismissThisViewController) withObject:nil afterDelay:1.5f];
        } else {
            [MBProgressHUD showToast:@"修改密码失败" initWithView:self.view];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
    }];
}

- (void)dismissThisViewController {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - Private Method
- (void)createUI {
    
    UIImageView *passwordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(12.5f), TRANS_VALUE(8.0f), TRANS_VALUE(15.0f), TRANS_VALUE(24.0f))];
    passwordImageView.contentMode = UIViewContentModeScaleAspectFit;
    passwordImageView.image = [UIImage imageNamed:@"ic_login_password_1"];
    [self.view addSubview:passwordImageView];
    
    UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(40.0f), TRANS_VALUE(0.0f), TRANS_VALUE(70.0f), TRANS_VALUE(40.0f))];
    passwordLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    passwordLabel.textColor = I_COLOR_BLACK;
    passwordLabel.text = @"输入新密码";
    [self.view addSubview:passwordLabel];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(120.0f), TRANS_VALUE(0.0f), TRANS_VALUE(200.0f), TRANS_VALUE(40.0f))];
    self.passwordTextField.borderStyle = UITextBorderStyleNone;
    self.passwordTextField.backgroundColor = I_COLOR_WHITE;
    self.passwordTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.passwordTextField.textColor = I_COLOR_BLACK;
    self.passwordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入新密码" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.passwordTextField.tintColor = I_COLOR_BLACK;
    self.passwordTextField.textAlignment = NSTextAlignmentLeft;
    [self.passwordTextField setSecureTextEntry:YES];
    [self.view addSubview:self.passwordTextField];
    
    UIView *dividerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f) + 1.0f, SCREEN_WIDTH, 1.0f)];
    dividerView1.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView1];
    
    self.commitButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(25.0f), TRANS_VALUE(60.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(25.0f), TRANS_VALUE(36.0f))];
    self.commitButton.clipsToBounds = YES;
    self.commitButton.layer.borderColor = I_COLOR_GRAY.CGColor;
    self.commitButton.layer.borderWidth = 1.0f;
    self.commitButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    [self.commitButton setTitleColor:I_COLOR_BLACK forState:UIControlStateNormal];
    [self.commitButton setTitleColor:I_COLOR_BLACK forState:UIControlStateSelected];
    [self.commitButton setTitle:@"提交修改" forState:UIControlStateNormal];
    [self.commitButton setTitle:@"提交修改" forState:UIControlStateSelected];
    [self.view addSubview:self.commitButton];
    
    [self.commitButton addTarget:self action:@selector(commitButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)loadData {
    
}

@end
